package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(summaryRanges([]int{0, 2, 3, 4, 6, 7, 9}))
}

func summaryRanges(nums []int) []string {
	if len(nums) == 0 {
		return []string{}
	}
	var ans []string
	nums = append(nums, nums[len(nums)-1]+2)
	last := nums[0]
	l := 0
	for r := 1; r < len(nums); r++ {
		c := nums[r]
		if c != last+1 {
			if nums[l] == last {
				ans = append(ans, strconv.Itoa(nums[l]))
			} else {
				ans = append(ans, strconv.Itoa(nums[l])+"->"+strconv.Itoa(last))
			}
			last = c
			l = r
		} else {
			last = c
		}
	}
	return ans
}

func summaryRanges3(nums []int) []string {
	if len(nums) == 0 {
		return []string{}
	}
	var ans []string

	for l, r := 0, 0; l < len(nums); l = r + 1 {
		r = l
		if r+1 < len(nums) && nums[r+1] == nums[r]-1 {
			r++
		}
		if nums[l] == nums[r] {
			ans = append(ans, strconv.Itoa(nums[l]))
		} else {
			ans = append(ans, strconv.Itoa(nums[l])+"->"+strconv.Itoa(nums[r]))
		}
	}
	return ans
}
